package com.blog.qiniu;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;

import lombok.extern.slf4j.Slf4j;

/** 
 * @author ant 
 * @date 创建时间：2017年3月28日 下午4:56:48  
 */
@Component
@Slf4j
public class Qiniu {	
	
	@Autowired
	private QiniuProperties qiniuProperties;
	
	/** 
	 * @Description: 以文件形式上传文件 
	 * @param localFile 本地文件
	 * @param fileName 文件名
	 * @param bucket 七牛云存储中的存储块
	 * @return boolean
	 */
	public boolean uploadFileWithFile(File localFile,String fileName,String bucket) {
		/**
		 * 构造一个带指定Zone对象的配置类
		 * 华北	Zone.zone1()
		 * 华南	Zone.zone2()
		 * 北美	Zone.zoneNa0()
		 */
		Configuration cfg = new Configuration(Zone.zone2());		
		UploadManager uploadManager = new UploadManager(cfg);
		Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
		String upToken = auth.uploadToken(bucket);
		try {
		    uploadManager.put(localFile, fileName, upToken);
		    log.info("上传文件成功");
            return true;
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    log.error(r.toString());
		    return false;
		}
	}
	
	/** 
	 * @Description: 以字节数组形式上传文件 
	 * @param localFilePath 本地文件的路径
	 * @param fileName 文件名
	 * @param bucket 七牛云存储中的存储块
	 * @return boolean
	 */
	public boolean uploadFileWithByteArray(byte[] uploadBytes,String fileName,String bucket) {
		/**
		 * 构造一个带指定Zone对象的配置类
		 * 华北	Zone.zone1()
		 * 华南	Zone.zone2()
		 * 北美	Zone.zoneNa0()
		 */
		Configuration cfg = new Configuration(Zone.zone2());		
		UploadManager uploadManager = new UploadManager(cfg);
		Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
		String upToken = auth.uploadToken(bucket);
		try {
		    uploadManager.put(uploadBytes, fileName, upToken);
		    //解析上传成功的结果
		    //DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		    log.info("上传文件成功");
            return true;
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    log.error(r.toString());
		    return false;
		}
	}
	
	/** 
	 * @Description: 以数据流形式上传文件 
	 * @param localFilePath 本地文件的路径
	 * @param fileName 文件名
	 * @param bucket 七牛云存储中的存储块
	 * @return boolean
	 */
	public boolean uploadFileWithInputStream(InputStream inputStream,String fileName,String bucket) {
		/**
		 * 构造一个带指定Zone对象的配置类
		 * 华北	Zone.zone1()
		 * 华南	Zone.zone2()
		 * 北美	Zone.zoneNa0()
		 */
		Configuration cfg = new Configuration(Zone.zone2());		
		UploadManager uploadManager = new UploadManager(cfg);
		Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
		String upToken = auth.uploadToken(bucket);
		try {
		    uploadManager.put(inputStream, fileName, upToken,null, null);
		    //解析上传成功的结果
		    //DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		    log.info("上传文件成功");
            return true;
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    log.error(r.toString());
		    return false;
		}
	}

	/** 
	 * @Description: 以断点续传形式上传文件 
	 * @param localFilePath 本地文件的路径
	 * @param fileName 文件名
	 * @param bucket 七牛云存储中的存储块
	 * @return boolean
	 */
	public boolean uploadFileWithBreakpoint(InputStream inputStream,String fileName,String bucket) {
		/**
		 * 构造一个带指定Zone对象的配置类
		 * 华北	Zone.zone1()
		 * 华南	Zone.zone2()
		 * 北美	Zone.zoneNa0()
		 */
		Configuration cfg = new Configuration(Zone.zone2());
		Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
		String upToken = auth.uploadToken(bucket);

		String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
		try {
		    //设置断点续传文件进度保存目录
		    FileRecorder fileRecorder = new FileRecorder(localTempDir);
		    UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
		    try {
		    	uploadManager.put(inputStream, fileName, upToken,null, null);
		        return true;
		    } catch (QiniuException ex) {
		        Response r = ex.response;
		        System.err.println(r.toString());
		        return false;		       
		    }
		} catch (IOException ex) {
		    ex.printStackTrace();
		    return false;
		}
	}
	
	/**
	 * @Description: 删除文件 
	 * @param bucket 七牛空间名
	 * @param fileName 文件名
	 * @return boolean
	 */
	public boolean deleteFile(String bucket,String fileName) {
        Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
        Configuration c = new Configuration(Zone.zone2());
        //实例化一个BucketManager对象
        BucketManager bucketManager = new BucketManager(auth, c);
        try {
            //调用delete方法移动文件
            bucketManager.delete(bucket, fileName);
            log.info("删除文件成功");
            return true;
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    log.error(r.toString());
		    return false;
        }
	}
}
